package object2D;

import context.*;

import model2D.Model2D;

import org.json.JSONObject;
import org.json.JSONException;

import util.FillStyle;
import util.StrokeStyle;
import util.Util2;

import geometry2D.MetricPoint2D;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;

// ********************************************************************
// *** CLASS: EllipseObject
// ********************************************************************
public class EllipseObject extends Object2D implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="object2D.EllipseObject";
  static final String TYPE="Ellipse";

  public EllipseObject(Model2D objectModel,MetricPoint2D location1,MetricPoint2D location2) throws JSONException
  { super(objectModel,null);
    stroke.setEnabled(true);
	add(location1);
	add(location2);
	this.setClosedPath(true);
  }
  
  public EllipseObject(Model2D objectModel,JSONObject jsonObject) throws JSONException
  { super(objectModel,jsonObject.getString("id"));
	if(DEBUG) Log.d(TAG,"<init>: jsonObject="+jsonObject);
    this.fill=new FillStyle(jsonObject);
    this.stroke=new StrokeStyle(jsonObject);
    clearPointSet();
    add(new MetricPoint2D(jsonObject.getJSONArray("p1")));
    add(new MetricPoint2D(jsonObject.getJSONArray("p2")));
    setClosedPath(true);
  }

  public JSONObject toJSONObject() throws JSONException
  { JSONObject jsonObject=new JSONObject();
    jsonObject.put("type",TYPE);
    jsonObject.put("id",getIdent());
    MetricPoint2D p1=getMapLocation(0);
	MetricPoint2D p2=getMapLocation(1);
    jsonObject.put("id",getIdent());
    jsonObject.put("p1",p1.encode());
    jsonObject.put("p2",p2.encode());
    jsonObject.put("fill",fill.encode());
    jsonObject.put("stroke",stroke.encode());
	return(jsonObject);
  }

  protected Shape getShape(double dx,double dy)
  { MetricPoint2D p0=getMapLocation(0);
    MetricPoint2D p1=getMapLocation(1);
    double x0=p0.getX()+dx;
    double y0=p0.getY()+dy;
    double x1=p1.getX()+dx;
    double y1=p1.getY()+dy;
    double x=Math.min(x0,x1);
    double y=Math.min(y0,y1);
    double width=Math.abs(x1-x0);
    double height=Math.abs(y1-y0);
    Ellipse2D shape=new Ellipse2D.Double(x,y,width,height);
    return(shape);	  
  }
  
  
  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    private JButton fillButton;
    
    public void paint(Graphics g)
    { if(DEBUG) Log.d(TAG,"EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
      fillButton.setBackground(fill.getColor());
  	super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util2.getTitleBorder(title));
  	  setPreferredSize(new Dimension(350,120));
      setLayout(new GridLayout(5,2));
      add(new JLabel("       Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      fillButton=addEditFillButton(this,true);
      strokeButton=addEditStrokeButton(this,true);
    }
 }

}
